package com.qf.service.impl;


import com.qf.dao.TransferDao;
import com.qf.dao.impl.TransferDaoImpl;
import com.qf.model.UserCard;
import com.qf.service.TransferService;

import java.math.BigDecimal;

public class TransferServiceImpl implements TransferService {
    private TransferDao dao = new TransferDaoImpl();

    //提现
    @Override
    public int withdrawal(String cid, BigDecimal amount) {

        return dao.withdrawal(cid,amount);
    }

    //充值
    @Override
    public int recharge(String cid, BigDecimal amount) {
        return dao.recharge(cid,amount);
    }

    //转账
    @Override
    public int transfer(String fromAccountId, String toAccountId, BigDecimal amount) {

        // 根据转出账户ID获取账户信息
        UserCard fromAccount = dao.getAccount(fromAccountId);
        // 根据转入账户ID获取账户信息
        UserCard toAccount = dao.getAccount(toAccountId);
        BigDecimal money1 = fromAccount.getMoney();
        int m = money1.compareTo(amount);//比较余额和转账金额

        if (toAccount != null) {
            if (m != -1) {
//dao.transfer(con,fromAccountId,toAccountId,amount);
                dao.outMoney(fromAccountId, amount);
                dao.inMoney(toAccountId, amount);
                return 1;
            }
        }
        return 0;

    }

}

